import 'package:fluent_ui/fluent_ui.dart';

import 'auth_model.dart';

///全局设置
class SettingModel {
  static const _baseUrlKey = 'baseUrl',
      _tenantKey = 'tenant',
      _accessTokenKey = 'access-token',
      _refreshTokenKey = 'refresh-token',
      _nickKey = "nick",
      _avatarKey = "avatar";

  String baseUrl, tenant, accessToken, refreshToken, nick, avatar;

  ///构造函数
  SettingModel({
    this.baseUrl = '',
    this.tenant = '',
    this.accessToken = '',
    this.refreshToken = '',
    this.nick = '',
    this.avatar = '',
  });

  SettingModel copyWith({
    String? baseUrl,
    String? tenant,
    String? accessToken,
    String? refreshToken,
    String? nick,
    String? avatar,
  }) =>
      SettingModel(
        baseUrl: baseUrl ?? this.baseUrl,
        tenant: tenant ?? this.tenant,
        accessToken: accessToken ?? this.accessToken,
        refreshToken: refreshToken ?? this.refreshToken,
        nick: nick ?? this.nick,
        avatar: avatar ?? this.avatar,
      );

  ///复制数据处理
  bool copy(final SettingModel model) {
    var isChange = false;
    if (model.baseUrl.isNotEmpty && model.baseUrl != baseUrl) {
      debugPrint('copy(baseUrl: $baseUrl => ${model.baseUrl})');
      baseUrl = model.baseUrl;
      isChange = true;
    }
    if (model.tenant.isNotEmpty) {
      debugPrint('copy(tenant: $tenant => ${model.tenant})');
      tenant = model.tenant;
      isChange = true;
    }
    if (model.accessToken != accessToken) {
      debugPrint('copy(accessToken: $accessToken => ${model.accessToken})');
      accessToken = model.accessToken;
      isChange = true;
    }
    if (model.refreshToken != refreshToken) {
      debugPrint('copy(refreshToken: $refreshToken => ${model.refreshToken})');
      refreshToken = model.refreshToken;
      isChange = true;
    }
    if (model.nick != nick) {
      debugPrint('copy(nick: $nick => ${model.nick})');
      nick = model.nick;
      isChange = true;
    }
    if (model.avatar != avatar) {
      debugPrint('copy(avatar: $avatar => ${model.avatar})');
      avatar = model.avatar;
      isChange = true;
    }
    return isChange;
  }

  ///是否存在BaseURL
  bool get hasBaseUrl => baseUrl.isNotEmpty;

  ///是否已登录
  bool get isAuth => accessToken.isNotEmpty && tenant.isNotEmpty;

  ///读取处理
  factory SettingModel.fromMap(final Map<String, dynamic> data) => SettingModel(
        baseUrl: data[_baseUrlKey] ?? '',
        tenant: data[_tenantKey] ?? '',
        accessToken: data[_accessTokenKey] ?? '',
        refreshToken: data[_refreshTokenKey] ?? '',
        nick: data[_nickKey] ?? '',
        avatar: data[_avatarKey] ?? '',
      );

  factory SettingModel.fromAuthRes(final AuthLoginRes res) {
    if (res.isSuccessed) {
      return SettingModel(
          tenant: res.tenantId,
          accessToken: res.accessToken,
          refreshToken: res.refreshToken,
          nick: res.nick ?? '',
          avatar: res.avatar ?? '');
    }
    return SettingModel();
  }

  ///转换为Map
  Map<String, dynamic> toMap() => {
        _baseUrlKey: baseUrl,
        _tenantKey: tenant,
        _accessTokenKey: accessToken,
        _refreshTokenKey: refreshToken,
        _nickKey: nick,
        _avatarKey: avatar,
      };
}
